package com.wutao.panzh.admin.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.panzh.entity.*;
import com.panzh.entity.index.Classification;
import com.panzh.entity.index.IndexFoodItem;
import com.panzh.entity.index.IndexSceneryItem;
import com.panzh.myconst.ProjectParames;
import com.wutao.panzh.admin.mapper.*;
import com.wutao.panzh.service.FoodItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FoodItemServiceImpl implements FoodItemService {

    @Autowired
    private FoodItemMapper foodItemMapper;

    @Autowired
    private SceneryClassificationMapper sceneryClassificationMapper;

    @Autowired
    private FoodClassificationMapper foodClassificationMapper;

    @Autowired
    private RouteClassificationMapper routeClassificationMapper;

    @Autowired
    private HotelClassificationMapper hotelClassificationMapper;


    @Override
    public FoodItem getFoodItemById(String foodItemId) {
        return foodItemMapper.selectByPrimaryKey(Integer.parseInt(foodItemId));
    }

    @Override
    public void deleteFoodItemById(String foodItemId) {
        foodItemMapper.deleteFoodImage(foodItemId);
        foodItemMapper.deleteByPrimaryKey(Integer.parseInt(foodItemId));
    }

    @Override
    public PageInfo<FoodItem> getFoodItemPageInfo(Integer pageNo, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNo,pageSize);
        List<FoodItem> foodItemList =  foodItemMapper.selectFroSearch(keyword);
        PageInfo<FoodItem> pageInfo = new PageInfo<>(foodItemList);
        return pageInfo;
    }

    @Override
    public FoodItem getFoodItemByItemName(String itemName) {
        FoodItem foodItem = new FoodItem();
        foodItem.setFoodName(itemName);
        FoodItem foodItem1 = foodItemMapper.selectOne(foodItem);
        return foodItem1;
    }

    @Override
    public void saveFoodItem(FoodItem foodItem) {
        String name = foodItem.getCategorizationInformationName();
        //从foodItem中获得分类的名称，然后在foodClassification中获得对应的实体类，因为只有一个，所以使用selectOne，然后把对应的分类id存入foodItem;
        FoodClassification foodClassification = new FoodClassification();
        foodClassification.setCategorizationInformation(name);
        FoodClassification resultFoodClassification = foodClassificationMapper.selectOne(foodClassification);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String dateFormat = simpleDateFormat.format(new Date());
        foodItem.setFoodData(dateFormat);
        foodItem.setCategorizationInformationId(resultFoodClassification.getId());
        foodItemMapper.insert(foodItem);
    }

    @Override
    public List<FoodItem> getFoodItemAllByItemName(String itemName) {
        FoodItem foodItem = new FoodItem();
        foodItem.setFoodName(itemName);
        List<FoodItem> result = foodItemMapper.select(foodItem);
        return result;
    }

    @Override
    public void updateFoodItem(FoodItem foodItem) {
        String name = foodItem.getCategorizationInformationName();
        //从foodItem中获得分类的名称，然后在foodClassification中获得对应的实体类，因为只有一个，所以使用selectOne，然后把对应的分类id存入foodItem;
        FoodClassification foodClassification = new FoodClassification();
        foodClassification.setCategorizationInformation(name);
        FoodClassification resultFoodClassification = foodClassificationMapper.selectOne(foodClassification);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String dateFormat = simpleDateFormat.format(new Date());
        foodItem.setFoodData(dateFormat);
        foodItem.setCategorizationInformationId(resultFoodClassification.getId());
        foodItemMapper.updateByPrimaryKey(foodItem);
    }

    @Override
    public void insertFoodImageSrc(String imagSrc, String foodItemId) {
        //先进行查询，如果查询有结果，就进行更新，如果查询没有结果，就进行插入；
        String imagSrcBySceneryItemId = foodItemMapper.selectImagSrcByFoodItemId(foodItemId);
        if (imagSrcBySceneryItemId == null){
            //进行插入；
            foodItemMapper.insertImagSrcByFoodItemId(imagSrc,foodItemId);
        }else {
            //进行更新；
            foodItemMapper.updateImagSrcByFoodItemId(imagSrc,foodItemId);
        }
    }

    @Override
    public HashMap<String, List<Classification>> getAllClassication() {
        /**
         * 向上转型；
         */
        HashMap<String, List<Classification>> map = new HashMap<>();

        List<SceneryClassification> sceneryClassifications = sceneryClassificationMapper.selectAll();
        List<Classification> listByScenery = new ArrayList<>();
        for (SceneryClassification classification : sceneryClassifications) {
            listByScenery.add(classification);
        }
        map.put("sceneryClassifications",listByScenery);

        List<FoodClassification> foodClassifications = foodClassificationMapper.selectAll();
        List<Classification> listByFood = new ArrayList<>();
        for (FoodClassification classification : foodClassifications) {
            listByFood.add(classification);
        }
        map.put("foodClassifications",listByFood);

        List<RouteClassification> routeClassifications = routeClassificationMapper.selectAll();
        List<Classification> listByRoute = new ArrayList<>();
        for (RouteClassification classification : routeClassifications) {
            listByRoute.add(classification);
        }
        map.put("routeClassifications",listByRoute);

        List<HotelClassification> hotelClassifications = hotelClassificationMapper.selectAll();
        List<Classification> listByHotel = new ArrayList<>();
        for (HotelClassification classification : hotelClassifications) {
            listByHotel.add(classification);
        }
        map.put("hotelClassifications",listByHotel);

        return map;
    }

    @Override
    public PageInfo<IndexFoodItem> getAllIndexFoodItem(Integer pageNo, Integer classificationId) {
        PageHelper.startPage(pageNo, ProjectParames.PAGE_SIZE);
        List<IndexFoodItem> informationItems = foodItemMapper.selectIndexFoodFroSearch(classificationId);
        //封装成PageInfo进行返回
        PageInfo<IndexFoodItem> pageInfo = new PageInfo<>(informationItems);
        return pageInfo;
    }
}
